<html>
<head>
  <title>Evernote Export</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="YXBJ Windows/603706 (zh-CN, DDL); Windows/10.0.0 (Win64); EDAMVersion=V2;"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="1519"/>

<div>
<span><div><div><font style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(255, 0, 0); font-weight: bold;">Thread 线程 - 01</span></font></div><div><font style="font-size: 18pt;"><span style="font-size: 18pt;"> </span></font></div><div><font style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(255, 0, 0); font-weight: bold;">线程创建方式：</span></font></div><div><font style="font-size: 16pt;"><br/></font></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><font style="font-size: 16pt;">import java.util.concurrent.Callable;</font></div><div><font style="font-size: 16pt;">import java.util.concurrent.ExecutionException;</font></div><div><font style="font-size: 16pt;">import java.util.concurrent.FutureTask;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">public class TestCreate {</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public static void main(String[] args) throws ExecutionException, InterruptedException {</font></div><div><font style="font-size: 16pt;">        new MyThread().start();</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        MyThread2 myThread2 = new MyThread2();</font></div><div><font style="font-size: 16pt;">        new Thread(myThread2).start();</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        //lambda 函数式接口 静态代理</font></div><div><font style="font-size: 16pt;">        new Thread(()-&gt;{</font></div><div><font style="font-size: 16pt;">            System.out.println(&quot;mythread4!&quot;);</font></div><div><font style="font-size: 16pt;">        }).start();</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        FutureTask&lt;Integer&gt; integerFutureTask = new FutureTask&lt;Integer&gt;(new MuThread3());</font></div><div><font style="font-size: 16pt;">        new Thread(integerFutureTask).start();</font></div><div><font style="font-size: 16pt;">        Integer integer = integerFutureTask.get();</font></div><div><font style="font-size: 16pt;">        System.out.println(integer);</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;">class MyThread extends Thread{</font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public void run() {</font></div><div><font style="font-size: 16pt;">        System.out.println(&quot;mythread1!&quot;);</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">class MyThread2 implements Runnable{</font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public void run() {</font></div><div><font style="font-size: 16pt;">        System.out.println(&quot;mythread2!&quot;);</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;">class MuThread3 implements Callable&lt;Integer&gt;{</font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public Integer call() throws Exception {</font></div><div><font style="font-size: 16pt;">        System.out.println(&quot;mythread3!&quot;);</font></div><div><font style="font-size: 16pt;">        return 100;</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div></div><div><font style="font-size: 18pt;"><br/></font></div><div><span style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(255, 0, 0); font-weight: bold;">一、线程状态</span></span></div><div><font style="font-size: 18pt;"><br/></font></div><div style="text-align: center;"><span style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(255, 0, 0); font-weight: bold;"><img src="./thread/Image.png" type="image/png" data-filename="Image.png" width="1142"/></span></span></div><div style="border: 0px; margin: 0px 0px 1em 1em; padding: 0px 0px 0px 1.5em; word-break: break-all; background-image: url(&quot;./assets/img/bullet.png&quot;); background-repeat: no-repeat; background-position: left 0.5em; text-align: left;"><div><span style="font-size: 16pt; font-weight: bold; line-height: 1.5em;">新建状态:</span></div><div style="border: 0px; margin: 0px; padding: 0px 10px; overflow-wrap: break-word; word-break: break-all;"><font style="font-size: 16pt;"><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;">使用</span><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;"> </span><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-weight: bold; line-height: 2em;">new</span><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;"> </span><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;">关键字和</span><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;"> </span><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-weight: bold; line-height: 2em;">Thread</span><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;"> </span><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;">类或其子类建立一个线程对象后，该线程对象就处于新建状态。它保持这个状态直到程序</span><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;"> </span><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-weight: bold; line-height: 2em;">start()</span><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;"> </span><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;">这个线程。</span></font></div></div><div style="border: 0px; margin: 0px 0px 1em 1em; padding: 0px 0px 0px 1.5em; word-break: break-all; background-image: url(&quot;./assets/img/bullet.png&quot;); background-repeat: no-repeat; background-position: left 0.5em; text-align: left;"><div><span style="font-size: 16pt; font-weight: bold; line-height: 1.5em;">就绪状态:</span></div><div style="border: 0px; margin: 0px; padding: 0px 10px; overflow-wrap: break-word; word-break: break-all;"><span style="border: 0px; overflow-wrap: break-word; word-break: break-all;"><span style="font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;">当线程对象调用了start()方法之后，该线程就进入就绪状态。就绪状态的线程处于就绪队列中，要等待JVM里线程调度器的调度。</span></span></div></div><div style="border: 0px; margin: 0px 0px 1em 1em; padding: 0px 0px 0px 1.5em; word-break: break-all; background-image: url(&quot;./assets/img/bullet.png&quot;); background-repeat: no-repeat; background-position: left 0.5em; text-align: left;"><div><span style="font-size: 16pt; font-weight: bold; line-height: 1.5em;">运行状态:</span></div><div style="border: 0px; margin: 0px; padding: 0px 10px; overflow-wrap: break-word; word-break: break-all;"><font style="font-size: 16pt;"><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;">如果就绪状态的线程获取 CPU 资源，就可以执行</span><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;"> </span><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-weight: bold; line-height: 2em;">run()</span><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;">，此时线程便处于运行状态。处于运行状态的线程最为复杂，它可以变为阻塞状态、就绪状态和死亡状态。</span></font></div></div><div style="border: 0px; margin: 0px 0px 1em 1em; padding: 0px 0px 0px 1.5em; word-break: break-all; background-image: url(&quot;./assets/img/bullet.png&quot;); background-repeat: no-repeat; background-position: left 0.5em; text-align: left;"><div><span style="font-size: 16pt; font-weight: bold; line-height: 1.5em;">阻塞状态:</span></div><div style="border: 0px; margin: 0px; padding: 0px 10px; overflow-wrap: break-word; word-break: break-all;"><span style="border: 0px; overflow-wrap: break-word; word-break: break-all;"><span style="font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;">如果一个线程执行了sleep（睡眠）、suspend（挂起）等方法，失去所占用资源之后，该线程就从运行状态进入阻塞状态。在睡眠时间已到或获得设备资源后可以重新进入就绪状态。可以分为三种：</span></span></div><div style="border: 0px; margin: 0px 0px 1em 1em; padding: 0px 0px 0px 1.5em; word-break: break-all; background-image: url(&quot;./assets/img/bullet.png&quot;); background-repeat: no-repeat; background-position: left 0.5em;"><div style="border: 0px; margin: 0px; padding: 0px 10px; overflow-wrap: break-word; word-break: break-all;"><span style="border: 0px; overflow-wrap: break-word; word-break: break-all;"><span style="font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;">等待阻塞：运行状态中的线程执行 wait() 方法，使线程进入到等待阻塞状态。</span></span></div></div><div style="border: 0px; margin: 0px 0px 1em 1em; padding: 0px 0px 0px 1.5em; word-break: break-all; background-image: url(&quot;./assets/img/bullet.png&quot;); background-repeat: no-repeat; background-position: left 0.5em;"><div style="border: 0px; margin: 0px; padding: 0px 10px; overflow-wrap: break-word; word-break: break-all;"><span style="border: 0px; overflow-wrap: break-word; word-break: break-all;"><span style="font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;">同步阻塞：线程在获取 synchronized 同步锁失败(因为同步锁被其他线程占用)。</span></span></div></div><div style="border: 0px; margin: 0px 0px 1em 1em; padding: 0px 0px 0px 1.5em; word-break: break-all; background-image: url(&quot;./assets/img/bullet.png&quot;); background-repeat: no-repeat; background-position: left 0.5em;"><div style="border: 0px; margin: 0px; padding: 0px 10px; overflow-wrap: break-word; word-break: break-all;"><span style="border: 0px; overflow-wrap: break-word; word-break: break-all;"><span style="font-size: 16pt; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;">其他阻塞：通过调用线程的 sleep() 或 join() 发出了 I/O 请求时，线程就会进入到阻塞状态。当sleep() 状态超时，join() 等待线程终止或超时，或者 I/O 处理完毕，线程重新转入就绪状态。</span></span></div></div></div><div style="text-align: left;"><div><font style="font-size: 16pt;"><span style="font-size: 16pt; font-weight: bold; line-height: 1.5em;">        死亡状态:</span></font></div><div style="border: 0px; margin: 0px; padding: 0px 10px; overflow-wrap: break-word; word-break: break-all;"><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; line-height: 2em;">          一个运行状态的线程完成任务或者其他终止条件发生时，该线程就切换到终止状态。</span></div><div><br/></div></div><div style="text-align: left;"><span style="font-size: 24px;"><span style="font-size: 24px; color: rgb(255, 0, 0); font-weight: bold;">常见方法</span></span></div><div style="text-align: left;"><br/></div><div style="border: 0px; margin: 0px; padding: 0px; overflow-wrap: break-word; word-break: break-all; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-size: 16pt; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">下表列出了Thread类的一些重要方法：</span></span></div><table style="border-collapse: collapse; min-width: 100%;"><colgroup><col style="width: 55px;"></col><col style="width: 802px;"></col></colgroup><tbody style="border: 0px; margin: 0px; padding: 0px;"><tr style="padding: 0px; background-color: rgb(246, 244, 240);"><td style="font-size: 13px; vertical-align: top; text-align: left; background-color: rgb(85, 85, 85); border: 1px solid rgb(85, 85, 85); width: 55px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;">序号</span></div></td><td style="font-size: 13px; vertical-align: top; text-align: center; background-color: rgb(85, 85, 85); border: 1px solid rgb(85, 85, 85); width: 802px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;">方法描述</span></div></td></tr><tr style="padding: 0px; background-color: rgb(255, 255, 255);"><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 55px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">1</span></div></td><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 802px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 2em;">public void start()</span></div><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">使该线程开始执行；</span><span style="background-color: rgb(255, 255, 102); font-size: 12px; color: black; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 2em;">Java</span><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;"> 虚拟机调用该线程的 run 方法。</span></div></td></tr><tr style="padding: 0px; background-color: rgb(246, 244, 240);"><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 55px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">2</span></div></td><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 802px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 2em;">public void run()</span></div><div style="font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">如果该线程是使用独立的 Runnable 运行对象构造的，则调用该 Runnable 对象的 run 方法；否则，该方法不执行任何操作并返回。</span></div></td></tr><tr style="padding: 0px; background-color: rgb(255, 255, 255);"><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 55px; padding: 8px;"><div style="font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">3</span></div></td><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 802px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 2em;">public final void setName(String name)</span></div><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">改变线程名称，使之与参数 name 相同。</span></div></td></tr><tr style="padding: 0px; background-color: rgb(246, 244, 240);"><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 55px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">4</span></div></td><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 802px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 2em;">public final void setPriority(int priority)</span></div><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;"> 更改线程的优先级。</span></div></td></tr><tr style="padding: 0px; background-color: rgb(255, 255, 255);"><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 55px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">5</span></div></td><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 802px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 2em;">public final void setDaemon(boolean on)</span></div><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">将该线程标记为守护线程或用户线程。</span></div></td></tr><tr style="padding: 0px; background-color: rgb(246, 244, 240);"><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 55px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">6</span></div></td><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 802px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 2em;">public final void join(long millisec)</span></div><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">等待该线程终止的时间最长为 millis 毫秒。</span></div></td></tr><tr style="padding: 0px; background-color: rgb(255, 255, 255);"><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 55px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">7</span></div></td><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 802px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 2em;">public void interrupt()</span></div><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">中断线程。</span></div></td></tr><tr style="padding: 0px; background-color: rgb(246, 244, 240);"><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 55px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">8</span></div></td><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 802px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 2em;">public final boolean isAlive()</span></div><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">测试线程是否处于活动状态。</span></div></td></tr></tbody></table><div style="border: 0px; margin: 0px; padding: 0px; overflow-wrap: break-word; word-break: break-all; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="border: 0px; overflow-wrap: break-word; word-break: break-all; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-size: 16pt; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">测试线程是否处于活动状态。 上述方法是被Thread对象调用的。下面的方法是Thread类的静态方法。</span></span></div><table style="border-collapse: collapse; min-width: 100%;"><colgroup><col style="width: 60px;"></col><col style="width: 795px;"></col></colgroup><tbody style="border: 0px; margin: 0px; padding: 0px;"><tr style="padding: 0px; background-color: rgb(246, 244, 240);"><td style="font-size: 13px; vertical-align: top; text-align: left; background-color: rgb(85, 85, 85); border: 1px solid rgb(85, 85, 85); width: 60px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;">序号</span></div></td><td style="font-size: 13px; vertical-align: top; text-align: center; background-color: rgb(85, 85, 85); border: 1px solid rgb(85, 85, 85); width: 795px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;">方法描述</span></div></td></tr><tr style="padding: 0px; background-color: rgb(255, 255, 255);"><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 60px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">1</span></div></td><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 795px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 2em;">public static void yield()</span></div><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">暂停当前正在执行的线程对象，并执行其他线程。</span></div></td></tr><tr style="padding: 0px; background-color: rgb(246, 244, 240);"><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 60px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">2</span></div></td><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 795px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 2em;">public static void sleep(long millisec)</span></div><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">在指定的毫秒数内让当前正在执行的线程休眠（暂停执行），此操作受到系统计时器和调度程序精度和准确性的影响。</span></div></td></tr><tr style="padding: 0px; background-color: rgb(255, 255, 255);"><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 60px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">3</span></div></td><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 795px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 2em;">public static boolean holdsLock(Object x)</span></div><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">当且仅当当前线程在指定的对象上保持监视器锁时，才返回 true。</span></div></td></tr><tr style="padding: 0px; background-color: rgb(246, 244, 240);"><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 60px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">4</span></div></td><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 795px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 2em;">public static Thread currentThread()</span></div><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">返回对当前正在执行的线程对象的引用。</span></div></td></tr><tr style="padding: 0px; background-color: rgb(255, 255, 255);"><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 60px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">5</span></div></td><td style="font-size: 13px; vertical-align: top; border: 1px solid rgb(212, 212, 212); width: 795px; padding: 8px;"><div style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="border: 0px; font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 2em;">public static void dumpStack()</span></div><div style="font-size: 12px;"><span style="font-size: 12px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 2em;">将当前线程的堆栈跟踪打印至标准错误流。</span></div></td></tr></tbody></table><div style="text-align: left;"><span style="font-size: 24px;"><br/></span></div><div style="text-align: left;"><span style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(255, 0, 0); font-weight: bold;">二、线程停止</span></span></div><div style="text-align: center;"><span style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(255, 0, 0); font-weight: bold;"><img src="./thread/Image [1].png" type="image/png" data-filename="Image.png" width="1042"/></span></span></div><div style="text-align: left;"><span style="font-size: 16pt;">示例代码：</span></div><div style="text-align: left;"><font style="font-size: 16pt;"><br/></font></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><font style="font-size: 16pt;">/**</font></div><div><font style="font-size: 16pt;">* 线程停止</font></div><div><font style="font-size: 16pt;">*</font></div><div><font style="font-size: 16pt;">*/</font></div><div><font style="font-size: 16pt;">public class TestStopThread implements Runnable{</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    private  Boolean flag = true; //标志位</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public static void main(String[] args) {</font></div><div><font style="font-size: 16pt;">        TestStopThread testStopThread = new TestStopThread();</font></div><div><font style="font-size: 16pt;">        //子线程</font></div><div><font style="font-size: 16pt;">        new Thread(testStopThread).start();</font></div><div><font style="font-size: 16pt;">        for (int i = 0; i &lt; 100; i++) {</font></div><div><font style="font-size: 16pt;">            System.out.println(Thread.currentThread().getName()+&quot; 执行了 &quot;+i);</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">            try {</font></div><div><font style="font-size: 16pt;">                Thread.sleep(1);</font></div><div><font style="font-size: 16pt;">            } catch (InterruptedException e) {</font></div><div><font style="font-size: 16pt;">                e.printStackTrace();</font></div><div><font style="font-size: 16pt;">            }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">            //主线程执行了90次，子线程该结束了 ， 此时仅仅把主线程后续的执行完毕即可</font></div><div><font style="font-size: 16pt;">            if (i==90){</font></div><div><font style="font-size: 16pt;">                testStopThread.stop();</font></div><div><font style="font-size: 16pt;">            }</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public void run() {</font></div><div><font style="font-size: 16pt;">        int  i = 0;</font></div><div><font style="font-size: 16pt;">        while (flag){</font></div><div><font style="font-size: 16pt;">            System.out.println(Thread.currentThread().getName()+&quot; 执行了&quot;+i++);</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">        System.out.println(Thread.currentThread().getName()+&quot; 线程结束了！&quot;);</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    /**</font></div><div><font style="font-size: 16pt;">     * 线程停止方法</font></div><div><font style="font-size: 16pt;">     */</font></div><div><font style="font-size: 16pt;">    public void stop(){</font></div><div><font style="font-size: 16pt;">        flag = false;</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div></div><div style="text-align: left;"><br/></div><div style="text-align: left;"><font style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(255, 0, 0); font-weight: bold;">三、线程睡眠</span></font></div><ul><li><div style="text-align: left;"><span style="font-size: 16pt;">倒计时</span></div></li><li><div style="text-align: left;"><span style="font-size: 16pt;">时钟</span></div></li></ul><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><font style="font-size: 16pt;">import java.text.SimpleDateFormat;</font></div><div><font style="font-size: 16pt;">import java.util.Date;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">/**</font></div><div><font style="font-size: 16pt;">* 1.倒计时</font></div><div><font style="font-size: 16pt;">* 2.获取时间</font></div><div><font style="font-size: 16pt;">*/</font></div><div><font style="font-size: 16pt;">public class TestSleepThread04 implements Runnable {</font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public void run() {</font></div><div><font style="font-size: 16pt;">        //倒计时</font></div><div><font style="font-size: 16pt;">        int i = 10 ;</font></div><div><font style="font-size: 16pt;">        while (i&gt;=0){</font></div><div><font style="font-size: 16pt;">            try {</font></div><div><font style="font-size: 16pt;">                Thread.sleep(1000);</font></div><div><font style="font-size: 16pt;">            } catch (InterruptedException e) {</font></div><div><font style="font-size: 16pt;">                e.printStackTrace();</font></div><div><font style="font-size: 16pt;">            }</font></div><div><font style="font-size: 16pt;">            System.out.println(Thread.currentThread().getName()+i);</font></div><div><font style="font-size: 16pt;">            i--;</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        //时间</font></div><div><font style="font-size: 16pt;">        Date date = new Date(System.currentTimeMillis());</font></div><div><font style="font-size: 16pt;">        while (true){</font></div><div><font style="font-size: 16pt;">            System.out.println(new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;).format(date));</font></div><div><font style="font-size: 16pt;">            try {</font></div><div><font style="font-size: 16pt;">                Thread.sleep(1000);</font></div><div><font style="font-size: 16pt;">            } catch (InterruptedException e) {</font></div><div><font style="font-size: 16pt;">                e.printStackTrace();</font></div><div><font style="font-size: 16pt;">            }</font></div><div><font style="font-size: 16pt;">            date = new Date(System.currentTimeMillis());</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public static void main(String[] args) {</font></div><div><font style="font-size: 16pt;">        new Thread(new TestSleepThread04(),&quot;倒计时 &quot;).start();</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div></div><div style="text-align: left;"><font color="#FF0000" style="font-size: 18pt;"><br/></font></div><div><font color="#FF0000" style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(255, 0, 0); font-weight: bold;">四、线程礼让</span></font></div><ul><li><div><span style="font-size: 16pt;">让出cpu使用权</span></div></li><li><div><span style="font-size: 16pt;">线程状态由运行态-&gt;阻塞态</span></div></li></ul><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><font style="font-size: 16pt;">/**</font></div><div><font style="font-size: 16pt;">* 线程礼让</font></div><div><font style="font-size: 16pt;">*      让出CPU的使用权</font></div><div><font style="font-size: 16pt;">*/</font></div><div><font style="font-size: 16pt;">public class TestYieldThread05 implements Runnable{</font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public void run() {</font></div><div><font style="font-size: 16pt;">        System.out.println(Thread.currentThread().getName()+&quot;线程开始&quot;);</font></div><div><font style="font-size: 16pt;">        Thread.yield();//线程礼让</font></div><div><font style="font-size: 16pt;">        System.out.println(Thread.currentThread().getName()+&quot;线程结束&quot;);</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public static void main(String[] args) {</font></div><div><font style="font-size: 16pt;">        new Thread(new TestYieldThread05(), &quot;a&quot;).start();</font></div><div><font style="font-size: 16pt;">        new Thread(new TestYieldThread05(), &quot;b&quot;).start();</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div></div><div><br/></div><div><span style="font-size: 16pt;"><span style="font-size: 16pt; color: rgb(255, 0, 0); font-family: &quot;Helvetica Neue&quot;; font-weight: bold;">五、线程合并</span></span></div><ul><li><div><span style="font-size: 21.3333px; font-family: &quot;Helvetica Neue&quot;;">插队</span></div></li></ul><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><font style="font-size: 16pt;">public class TestJoinThread06 implements Runnable {</font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public void run() {</font></div><div><font style="font-size: 16pt;">        for (int i = 0; i &lt; 100; i++) {</font></div><div><font style="font-size: 16pt;">            System.out.println(Thread.currentThread().getName() + &quot; VIP! &quot;+i);</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public static void main(String[] args) throws InterruptedException {</font></div><div><font style="font-size: 16pt;">        //vip线程</font></div><div><font style="font-size: 16pt;">        TestJoinThread06 testJoinThread06 = new TestJoinThread06();</font></div><div><font style="font-size: 16pt;">        Thread t1 = new Thread(testJoinThread06, &quot;特权&quot;);</font></div><div><font style="font-size: 16pt;">        t1.start();</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        for (int i = 0; i &lt; 50; i++) {</font></div><div><font style="font-size: 16pt;">            if (i==20){</font></div><div><font style="font-size: 16pt;">                t1.join();</font></div><div><font style="font-size: 16pt;">            }</font></div><div><font style="font-size: 16pt;">            System.out.println(Thread.currentThread().getName()+&quot; 执行了 &quot;+i);</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div></div><div><font color="#FF0000" style="font-size: 16pt;"><br/></font></div><div><font color="#FF0000" style="font-size: 16pt;"><span style="font-size: 16pt; color: rgb(255, 0, 0); font-weight: bold;">六、线程状态</span></font></div><ul><li><div>NEW</div></li><li><div>RUNNABLE</div></li><li><div>TIME_WAITING</div></li><li><div>TERMINATED</div></li></ul><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><font style="font-size: 16pt;">public class TestStateThread07 implements  Runnable{</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public static void main(String[] args) throws InterruptedException {</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        Thread a = new Thread(new TestStateThread07(), &quot;a&quot;);</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        //new  ， NEW ,新生状态</font></div><div><font style="font-size: 16pt;">        Thread.State state = a.getState();</font></div><div><font style="font-size: 16pt;">        System.out.println(state);</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        //开启线程， RUNNABLE 运行状态</font></div><div><font style="font-size: 16pt;">        a.start();</font></div><div><font style="font-size: 16pt;">        state = a.getState();</font></div><div><font style="font-size: 16pt;">        System.out.println(state);</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        //子线程睡眠  ，TMED_WAITING 阻塞状态</font></div><div><font style="font-size: 16pt;">        while (a.getState() != Thread.State.TERMINATED){</font></div><div><font style="font-size: 16pt;">            Thread.sleep(1);</font></div><div><font style="font-size: 16pt;">            state = a.getState();</font></div><div><font style="font-size: 16pt;">            System.out.println(state);</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        //循环结束后，子线程a 死亡，状态变更为 TERMINATED terminated</font></div><div><font style="font-size: 16pt;">        state = a.getState();</font></div><div><font style="font-size: 16pt;">        System.out.println(state);</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public void run() {</font></div><div><font style="font-size: 16pt;">        try {</font></div><div><font style="font-size: 16pt;">            Thread.sleep(10);</font></div><div><font style="font-size: 16pt;">        } catch (InterruptedException e) {</font></div><div><font style="font-size: 16pt;">            e.printStackTrace();</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div></div><div><br/></div><div><font style="font-size: 16pt;"><span style="font-size: 16pt; color: rgb(255, 0, 0); font-weight: bold;">七、线程优先级</span></font></div><ul><li><div><span style="font-size: 16pt;">默认优先级 5 Thread.NORM_PRIORITY</span></div></li><li><div><span style="font-size: 16pt;">1  Thread.MIN_PRIORITY;</span></div></li><li><div><span style="font-size: 16pt;">5  Thread.NORM_PRIORITY;</span></div></li><li><div><span style="font-size: 16pt;">10 Thread.MAX_PRIORITY;</span></div></li></ul><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><font style="font-size: 16pt;">public class TestPriorityThread08 implements Runnable {</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public static void main(String[] args) {</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        Thread a1 = new Thread(new TestPriorityThread08(), &quot;a&quot;);</font></div><div><font style="font-size: 16pt;">        Thread a2 = new Thread(new TestPriorityThread08(), &quot;b&quot;);</font></div><div><font style="font-size: 16pt;">        Thread a3 = new Thread(new TestPriorityThread08(), &quot;c&quot;);</font></div><div><font style="font-size: 16pt;">        Thread a4 = new Thread(new TestPriorityThread08(), &quot;d&quot;);</font></div><div><font style="font-size: 16pt;">        Thread a5 = new Thread(new TestPriorityThread08(), &quot;e&quot;);</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        //先设置优先级 ，在启动线程</font></div><div><font style="font-size: 16pt;">        a1.setPriority(Thread.MIN_PRIORITY);</font></div><div><font style="font-size: 16pt;">        a2.setPriority(3);</font></div><div><font style="font-size: 16pt;">        a3.setPriority(Thread.NORM_PRIORITY);</font></div><div><font style="font-size: 16pt;">        a4.setPriority(7);</font></div><div><font style="font-size: 16pt;">        a5.setPriority(Thread.MAX_PRIORITY);</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        a1.start();</font></div><div><font style="font-size: 16pt;">        a2.start();</font></div><div><font style="font-size: 16pt;">        a3.start();</font></div><div><font style="font-size: 16pt;">        a4.start();</font></div><div><font style="font-size: 16pt;">        a5.start();</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        System.out.println(Thread.currentThread().getName() + &quot;==&quot; + Thread.currentThread().getPriority());</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public void run() {</font></div><div><font style="font-size: 16pt;">        System.out.println(Thread.currentThread().getName() + &quot;==&quot; + Thread.currentThread().getPriority());</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div></div><div><br/></div><div><font style="font-size: 16pt;"><span style="font-size: 16pt; color: rgb(255, 0, 0); font-weight: bold;">八、守护线程</span></font></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-size: 16pt; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">在Java中有两类线程：用户线程 (User Thread)、守护线程 (Daemon Thread)。 </span></span></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-align: left;"><span style="text-indent: 0px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-size: 16pt; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">所谓守护 线程，是指在程序运行的时候在后台提供一种通用服务的线程，比如垃圾回收线程就是一个很称职的守护者，并且这种线程并不属于程序中不可或缺的部分。因此，当所有的非守护线程结束时，程序也就终止了，同时会杀死进程中的所有守护线程。反过来说，只要任何非守护线程还在运行，程序就不会终止。</span></span></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-align: left;"><span style="text-indent: 0px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-size: 16pt; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">用户线程和守护线程两者几乎没有区别，唯一的不同之处就在于虚拟机的离开：如果用户线程已经全部退出运行了，只剩下守护线程存在了，虚拟机也就退出了。 因为没有了被守护者，守护线程也就没有工作可做了，也就没有继续运行程序的必要了。</span></span></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-align: left;"><span style="text-indent: 0px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="background-color: rgb(255, 255, 255); font-size: 16pt; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">将线程转换为守护线程可以通过调用Thread对象的setDaemon(true)方法来实现。在使用守护线程时需要注意一下几点：</span></span></div><ul><li><div style="margin: 10px auto; padding: 0px; text-indent: 0px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-align: left;"><span style="text-indent: 0px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-size: 16pt; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">(1) thread.setDaemon(true)必须在thread.start()之前设置，否则会跑出一个IllegalThreadStateException异常。你不能把正在运行的常规线程设置为守护线程。 </span></span></div></li><li><div style="margin: 10px auto; padding: 0px; text-indent: 0px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-align: left;"><span style="text-indent: 0px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-size: 16pt; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">(2) 在Daemon线程中产生的新线程也是Daemon的。</span></span></div></li><li><div style="margin: 10px auto; padding: 0px; text-indent: 0px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-align: left;"><span style="text-indent: 0px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-size: 16pt; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">(3) 守护线程应该永远不去访问固有资源，如文件、数据库，因为它会在任何时候甚至在一个操作的中间发生中断。</span></span></div></li></ul><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><font style="font-size: 16pt;">/**</font></div><div><font style="font-size: 16pt;">* 守护线程</font></div><div><font style="font-size: 16pt;">* 用户线程</font></div><div><font style="font-size: 16pt;">*/</font></div><div><font style="font-size: 16pt;">public class TestDeamonThread {</font></div><div><font style="font-size: 16pt;">    public static void main(String[] args) {</font></div><div><font style="font-size: 16pt;">        Thread you = new Thread(new You(), &quot;小米&quot;);</font></div><div><font style="font-size: 16pt;">        Thread god = new Thread(new God(), &quot;上帝&quot;);</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        god.setDaemon(true); // 设置该线程为守护线程，之后其他所有线程都结束时候，它才会结束</font></div><div><font style="font-size: 16pt;">        you.start();</font></div><div><font style="font-size: 16pt;">        god.start();</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">class  You implements Runnable{</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public void run() {</font></div><div><font style="font-size: 16pt;">        for (int i = 0; i &lt; 36500; i++) {</font></div><div><font style="font-size: 16pt;">            System.out.println(&quot;活了&quot;+i+&quot;天&quot;);</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">        System.out.println(&quot;baybay!&quot;);</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">class  God implements Runnable{</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public void run() {</font></div><div><font style="font-size: 16pt;">            while (true){</font></div><div><font style="font-size: 16pt;">                System.out.println(&quot;守护线程！&quot;);</font></div><div><font style="font-size: 16pt;">            }</font></div><div><font style="font-size: 16pt;">    }</font></div></div><div><br/></div></div><div><br/></div></span>
</div>
<hr>
<a name="1527"/>

<div>
<span><div><div><span style="font-size: 18pt; color: rgb(255, 0, 0); font-weight: bold;">Thread 线程 - 02  线程安全问题</span></div><div><font style="font-size: 18pt;"> </font></div><div><span style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(255, 0, 0); font-weight: bold;">一、线程不安全的例子</span></span></div><ul><li><div><span style="font-size: 18pt;"><span style="font-size: 18pt; font-weight: bold;">售票</span></span></div></li></ul><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><font color="#000000" style="font-size: 16pt;">/**</font></div><div><font color="#000000" style="font-size: 16pt;">* 买票案例</font></div><div><font color="#000000" style="font-size: 16pt;">*/</font></div><div><font color="#000000" style="font-size: 16pt;">public class TestUnSecurity {</font></div><div><font color="#000000" style="font-size: 16pt;">    public static void main(String[] args) {</font></div><div><font color="#000000" style="font-size: 16pt;">        TicketSale ticketSale = new TicketSale();</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">        new Thread(ticketSale, &quot;小红&quot;).start();</font></div><div><font color="#000000" style="font-size: 16pt;">        new Thread(ticketSale, &quot;小马&quot;).start();</font></div><div><font color="#000000" style="font-size: 16pt;">        new Thread(ticketSale, &quot;黄牛&quot;).start();</font></div><div><font color="#000000" style="font-size: 16pt;">    }</font></div><div><font color="#000000" style="font-size: 16pt;">}</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">class TicketSale implements Runnable {</font></div><div><font color="#000000" style="font-size: 16pt;">    //票数</font></div><div><font color="#000000" style="font-size: 16pt;">    private Integer ticketNum = 10;</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">    //标志</font></div><div><font color="#000000" style="font-size: 16pt;">    private boolean flag = true;</font></div><div><font color="#000000" style="font-size: 16pt;">    public TicketSale() {</font></div><div><font color="#000000" style="font-size: 16pt;">    }</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">    public TicketSale(Integer ticketNum) {</font></div><div><font color="#000000" style="font-size: 16pt;">        this.ticketNum = ticketNum;</font></div><div><font color="#000000" style="font-size: 16pt;">    }</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">    @Override</font></div><div><font color="#000000" style="font-size: 16pt;">    public void run() {</font></div><div><font color="#000000" style="font-size: 16pt;">        while (flag) {</font></div><div><font color="#000000" style="font-size: 16pt;">            try {</font></div><div><font color="#000000" style="font-size: 16pt;">                buy();</font></div><div><font color="#000000" style="font-size: 16pt;">            } catch (InterruptedException e) {</font></div><div><font color="#000000" style="font-size: 16pt;">                e.printStackTrace();</font></div><div><font color="#000000" style="font-size: 16pt;">            }</font></div><div><font color="#000000" style="font-size: 16pt;">        }</font></div><div><font color="#000000" style="font-size: 16pt;">    }</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">    //改进：将共享操作的方法进行互斥锁操作 synchronized</font></div><div><font color="#000000" style="font-size: 16pt;">    public synchronized void buy() throws InterruptedException {</font></div><div><font color="#000000" style="font-size: 16pt;">        if (ticketNum &lt;= 0) {</font></div><div><font color="#000000" style="font-size: 16pt;">            flag = false;</font></div><div><font color="#000000" style="font-size: 16pt;">            return;</font></div><div><font color="#000000" style="font-size: 16pt;">        }</font></div><div><font color="#000000" style="font-size: 16pt;">        System.out.println(Thread.currentThread().getName() + &quot; 买到了 &quot; + ticketNum);</font></div><div><font color="#000000" style="font-size: 16pt;">        ticketNum--;</font></div><div><font color="#000000" style="font-size: 16pt;">        Thread.sleep(100);</font></div><div><font color="#000000" style="font-size: 16pt;">    }</font></div><div><font color="#000000" style="font-size: 16pt;">}</font></div></div><ul><li><div><font style="font-size: 16pt;"><span style="font-size: 16pt; font-weight: bold;">银行取款</span></font></div></li></ul><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><font color="#000000" style="font-size: 16pt;">/**</font></div><div><font color="#000000" style="font-size: 16pt;">* 取钱案例</font></div><div><font color="#000000" style="font-size: 16pt;">*/</font></div><div><font color="#000000" style="font-size: 16pt;">public class TestUnSecurity02 {</font></div><div><font color="#000000" style="font-size: 16pt;">    public static void main(String[] args) {</font></div><div><font color="#000000" style="font-size: 16pt;">        Account account = new Account(&quot;结婚基金&quot;, 1000);</font></div><div><font color="#000000" style="font-size: 16pt;">        new WithDrawing(account , 100 ,&quot;老婆&quot;).start();</font></div><div><font color="#000000" style="font-size: 16pt;">        new WithDrawing(account , 50 ,&quot;老公&quot;).start();</font></div><div><font color="#000000" style="font-size: 16pt;">    }</font></div><div><font color="#000000" style="font-size: 16pt;">}</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">class Account {</font></div><div><font color="#000000" style="font-size: 16pt;">    String accountName;</font></div><div><font color="#000000" style="font-size: 16pt;">    Integer accountMoney;</font></div><div><font color="#000000" style="font-size: 16pt;">    public Account(String accountName, Integer accountMoney) {</font></div><div><font color="#000000" style="font-size: 16pt;">        this.accountName = accountName;</font></div><div><font color="#000000" style="font-size: 16pt;">        this.accountMoney = accountMoney;</font></div><div><font color="#000000" style="font-size: 16pt;">    }</font></div><div><font color="#000000" style="font-size: 16pt;">}</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">class WithDrawing extends Thread {</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">    private Account account; //取款的账户信息</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">    private Integer withDrawNum; //取款数</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">    private Integer nowMoney = 0; //已取得钱数</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">    public WithDrawing(Account account, Integer withDrawNum, String threadName) {</font></div><div><font color="#000000" style="font-size: 16pt;">        super(threadName);</font></div><div><font color="#000000" style="font-size: 16pt;">        this.account = account;</font></div><div><font color="#000000" style="font-size: 16pt;">        this.withDrawNum = withDrawNum;</font></div><div><font color="#000000" style="font-size: 16pt;">    }</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">    @Override</font></div><div><font color="#000000" style="font-size: 16pt;">    public void run() {</font></div><div><font color="#000000" style="font-size: 16pt;">        //同步代码块，将共享操作的对象进行锁定，变化的量进行锁定  synchronized(Object){}</font></div><div><font color="#000000" style="font-size: 16pt;">      synchronized (account){</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">          if (account.accountMoney &lt; withDrawNum) {</font></div><div><font color="#000000" style="font-size: 16pt;">              System.out.println(&quot;账户：&quot; + account.accountName + &quot;余额不足！&quot; + account.accountMoney);</font></div><div><font color="#000000" style="font-size: 16pt;">              return;</font></div><div><font color="#000000" style="font-size: 16pt;">          }</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">          account.accountMoney = account.accountMoney - withDrawNum;</font></div><div><font color="#000000" style="font-size: 16pt;">          nowMoney = nowMoney + withDrawNum;</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">          System.out.println(Thread.currentThread().getName()+&quot;在账户：&quot;+account.accountName+&quot;取款了：&quot;+withDrawNum);</font></div><div><font color="#000000" style="font-size: 16pt;">          System.out.println(Thread.currentThread().getName()+&quot;手里有：&quot;+nowMoney);</font></div><div><font color="#000000" style="font-size: 16pt;">          System.out.println(&quot;账户：&quot;+account.accountName+&quot;余额：&quot;+account.accountMoney);</font></div><div><font color="#000000" style="font-size: 16pt;">      }</font></div><div><font color="#000000" style="font-size: 16pt;">    }</font></div></div><ul><li><div><font style="font-size: 16pt;"><span style="font-size: 16pt; font-weight: bold;">List集合</span></font></div></li></ul><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><font color="#000000" style="font-size: 16pt;">import java.util.ArrayList;</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">/**</font></div><div><font color="#000000" style="font-size: 16pt;">* 多线程操作集合时候，出现覆盖现象</font></div><div><font color="#000000" style="font-size: 16pt;">*/</font></div><div><font color="#000000" style="font-size: 16pt;">public class TestUnSecurity03 {</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">    public static void main(String[] args) {</font></div><div><font color="#000000" style="font-size: 16pt;">        ArrayList&lt;String&gt; threads = new ArrayList&lt;&gt;();</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">        for (int i = 0; i &lt; 10000; i++) {</font></div><div><font color="#000000" style="font-size: 16pt;">            new Thread(() -&gt; {</font></div><div><font color="#000000" style="font-size: 16pt;">                //改进：将共享的数据，可变的对象加锁</font></div><div><font color="#000000" style="font-size: 16pt;">                synchronized (threads) { //对象 curd的对象</font></div><div><font color="#000000" style="font-size: 16pt;">                    threads.add(Thread.currentThread().getName());</font></div><div><font color="#000000" style="font-size: 16pt;">                }</font></div><div><font color="#000000" style="font-size: 16pt;">            }).start();</font></div><div><font color="#000000" style="font-size: 16pt;">        }</font></div><div><font color="#000000"><br/></font></div><div><font color="#000000"><br/></font></div><div><font color="#000000" style="font-size: 16pt;">        try {</font></div><div><font color="#000000" style="font-size: 16pt;">            Thread.sleep(3000);</font></div><div><font color="#000000" style="font-size: 16pt;">        } catch (InterruptedException e) {</font></div><div><font color="#000000" style="font-size: 16pt;">            e.printStackTrace();</font></div><div><font color="#000000" style="font-size: 16pt;">        }</font></div><div><font color="#000000" style="font-size: 16pt;">        System.out.println(threads.size());</font></div><div><font color="#000000" style="font-size: 16pt;">    }</font></div><div><font color="#000000" style="font-size: 16pt;">}</font></div></div><div><span style="font-size: 18pt;"><br/></span></div><div><span style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(255, 0, 0); font-weight: bold;">二、死锁</span></span></div><div><span style="font-size: 16pt;">产生死锁的四个必要条件:</span></div><ul><li><div><span style="font-size: 16pt;">1. 互斥条件：一个资源每次只能被一个进程使用。</span></div></li><li><div><span style="font-size: unset;"><span style="font-size: 16pt; color: unset; font-family: unset;">2. 请求与保持条件</span></span><span style="font-size: 16pt;">：</span><span style="font-size: unset;"><span style="font-size: 16pt; color: unset; font-family: unset;">一个进程因请求资源而阻塞时，对已获得的资源保持不放</span></span></div></li><li><div><span style="font-size: unset;"><span style="font-size: 16pt; color: unset; font-family: unset;">3. 不剥夺条件</span></span><span style="font-size: 16pt;">：</span><span style="font-size: unset;"><span style="font-size: 16pt; color: unset; font-family: unset;">进程已获得的资源，在末使用完之前，不能强行剥夺。</span></span></div></li><li><div><span style="font-size: unset;"><span style="font-size: 16pt; color: unset; font-family: unset;">4. 循环等待条件</span></span><span style="font-size: 16pt;">：</span><span style="font-size: unset;"><span style="font-size: 16pt; color: unset; font-family: unset;">若干进程之间形成一种头尾相接的循环等待资源关系。</span></span></div></li><li><div><span style="font-size: 16pt;">破坏上述一个或多个条件，死锁就不会产生</span></div></li></ul><div><span style="font-size: 16pt;"><br/></span></div><div><span style="font-size: 16pt;">示例代码：</span></div><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><font style="font-size: 16pt;">/**</font></div><div><font style="font-size: 16pt;">* 死锁：</font></div><div><font style="font-size: 16pt;">* 产生原因：线程之间获取对方持有且不释放的资源</font></div><div><font style="font-size: 16pt;">*/</font></div><div><font style="font-size: 16pt;">public class TestDeadLock {</font></div><div><font style="font-size: 16pt;">    public static void main(String[] args) {</font></div><div><font style="font-size: 16pt;">        new MakeUp(&quot;灰姑娘&quot;, 0).start();</font></div><div><font style="font-size: 16pt;">        new MakeUp(&quot;白雪公主&quot;, 1).start();</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">class LipStick {</font></div><div><font style="font-size: 16pt;">    //口红</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">class Mirror {</font></div><div><font style="font-size: 16pt;">    //镜子</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">/**</font></div><div><font style="font-size: 16pt;">* 化妆</font></div><div><font style="font-size: 16pt;">*/</font></div><div><font style="font-size: 16pt;">class MakeUp extends Thread {</font></div><div><font style="font-size: 16pt;">    private String name;</font></div><div><font style="font-size: 16pt;">    private Integer choose;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public MakeUp(String name, Integer choose) {</font></div><div><font style="font-size: 16pt;">        super(name);</font></div><div><font style="font-size: 16pt;">        this.name = name;</font></div><div><font style="font-size: 16pt;">        this.choose = choose;</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public void run() {</font></div><div><font style="font-size: 16pt;">        try {</font></div><div><font style="font-size: 16pt;">            makeup();</font></div><div><font style="font-size: 16pt;">        } catch (InterruptedException e) {</font></div><div><font style="font-size: 16pt;">            e.printStackTrace();</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    //确保唯一 static</font></div><div><font style="font-size: 16pt;">    static LipStick lipStick = new LipStick();</font></div><div><font style="font-size: 16pt;">    static Mirror mirror = new Mirror();</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    /**</font></div><div><font style="font-size: 16pt;">     * 解决该死锁办法，两个同步锁 不要嵌套， 当一个线程使用该锁后，释放，另一个线程在去获取，就可以拿到资源，死锁就不会产生</font></div><div><font style="font-size: 16pt;">     * @throws InterruptedException</font></div><div><font style="font-size: 16pt;">     */</font></div><div><font style="font-size: 16pt;">    public void makeup() throws InterruptedException {</font></div><div><font style="font-size: 16pt;">        if (choose == 0) {</font></div><div><font style="font-size: 16pt;">            //想要镜子</font></div><div><font style="font-size: 16pt;">            synchronized (lipStick) {</font></div><div><font style="font-size: 16pt;">                System.out.println(Thread.currentThread().getName() + &quot;拿到了镜子的锁&quot;);</font></div><div><font style="font-size: 16pt;">                Thread.sleep(1000);</font></div><div><font style="font-size: 16pt;">                //想要口红  //改进：不要嵌套</font></div><div><font style="font-size: 16pt;">                synchronized (mirror) {</font></div><div><font style="font-size: 16pt;">                    System.out.println(Thread.currentThread().getName() + &quot;拿到了口红的锁&quot;);</font></div><div><font style="font-size: 16pt;">                }</font></div><div><font style="font-size: 16pt;">            }</font></div><div><font style="font-size: 16pt;">        }else{</font></div><div><font style="font-size: 16pt;">            //想要口红</font></div><div><font style="font-size: 16pt;">            synchronized (mirror) {</font></div><div><font style="font-size: 16pt;">                System.out.println(Thread.currentThread().getName() + &quot;拿到了口红的锁&quot;);</font></div><div><font style="font-size: 16pt;">                Thread.sleep(2000);</font></div><div><font style="font-size: 16pt;">                //想要镜子  //改进：不要嵌套</font></div><div><font style="font-size: 16pt;">                synchronized (lipStick) {</font></div><div><font style="font-size: 16pt;">                    System.out.println(Thread.currentThread().getName() + &quot;拿到了镜子的锁&quot;);</font></div><div><font style="font-size: 16pt;">                }</font></div><div><font style="font-size: 16pt;">            }</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div></div><div><span style="font-size: 16pt;"><br/></span></div><div><span style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(255, 0, 0); font-weight: bold;">三、线程通信</span></span></div></div><div><span style="font-size: 18pt;"><span style="font-size: 18pt;"><br/></span></span></div><div><span style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(255, 0, 0); font-weight: bold;">1.管程法</span></span></div><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><font style="font-size: 16pt;">import com.sun.corba.se.impl.orbutil.concurrent.Sync;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">/**</font></div><div><font style="font-size: 16pt;">* 线程通信： 管程法</font></div><div><font style="font-size: 16pt;">*/</font></div><div><font style="font-size: 16pt;">public class TestProductConsumer {</font></div><div><font style="font-size: 16pt;">    public static void main(String[] args) {</font></div><div><font style="font-size: 16pt;">        SynContainer container = new SynContainer();</font></div><div><font style="font-size: 16pt;">        new Producer(container,&quot;生产者&quot;).start();</font></div><div><font style="font-size: 16pt;">        new Consumer(container,&quot;消费者&quot;).start();</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">/**</font></div><div><font style="font-size: 16pt;">* 生产者</font></div><div><font style="font-size: 16pt;">*/</font></div><div><font style="font-size: 16pt;">class Producer extends Thread {</font></div><div><font style="font-size: 16pt;">    private SynContainer synContainer ;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public Producer(SynContainer synContainer,String name) {</font></div><div><font style="font-size: 16pt;">        super(name);</font></div><div><font style="font-size: 16pt;">        this.synContainer = synContainer;</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public void run() {</font></div><div><font style="font-size: 16pt;">        for (int i = 0; i &lt; 100; i++) {</font></div><div><font style="font-size: 16pt;">            synContainer.push(new Chicken(i));</font></div><div><font style="font-size: 16pt;">            System.out.println(Thread.currentThread().getName()+&quot; 生产了 &quot;+i+&quot; 鸡&quot;);</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">/**</font></div><div><font style="font-size: 16pt;">* 消费者</font></div><div><font style="font-size: 16pt;">*/</font></div><div><font style="font-size: 16pt;">class Consumer extends Thread {</font></div><div><font style="font-size: 16pt;">    private SynContainer synContainer ;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public Consumer(SynContainer synContainer,String name) {</font></div><div><font style="font-size: 16pt;">        super(name);</font></div><div><font style="font-size: 16pt;">        this.synContainer = synContainer;</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public void run() {</font></div><div><font style="font-size: 16pt;">        for (int i = 0; i &lt; 100; i++) {</font></div><div><font style="font-size: 16pt;">            Chicken pop = synContainer.pop(new Chicken(i));</font></div><div><font style="font-size: 16pt;">            System.out.println(Thread.currentThread().getName()+&quot; 消费了 &quot;+pop.i+&quot; 鸡&quot;);</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">/**</font></div><div><font style="font-size: 16pt;">* 产品</font></div><div><font style="font-size: 16pt;">*/</font></div><div><font style="font-size: 16pt;">class Chicken {</font></div><div><font style="font-size: 16pt;">    int i;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public Chicken(int i) {</font></div><div><font style="font-size: 16pt;">        this.i = i;</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">/**</font></div><div><font style="font-size: 16pt;">* 管程法</font></div><div><font style="font-size: 16pt;">*/</font></div><div><font style="font-size: 16pt;">class SynContainer {</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    //容器</font></div><div><font style="font-size: 16pt;">    Chicken[] chickens = new Chicken[10];</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    //计算器</font></div><div><font style="font-size: 16pt;">    int count = 0 ;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public synchronized void push(Chicken chicken){</font></div><div><font style="font-size: 16pt;">        if (chickens.length-1 &lt;= count){</font></div><div><font style="font-size: 16pt;">            //容器满</font></div><div><font style="font-size: 16pt;">            try {</font></div><div><font style="font-size: 16pt;">                this.wait(); //阻塞该线程</font></div><div><font style="font-size: 16pt;">            } catch (InterruptedException e) {</font></div><div><font style="font-size: 16pt;">                e.printStackTrace();</font></div><div><font style="font-size: 16pt;">            }</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">        //存放产品</font></div><div><font style="font-size: 16pt;">        chickens[count] = chicken;</font></div><div><font style="font-size: 16pt;">        count++ ;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        //产品有了，通知消费者</font></div><div><font style="font-size: 16pt;">        this.notify(); //唤醒其他线程</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public synchronized Chicken pop(Chicken chicken){</font></div><div><font style="font-size: 16pt;">        if (count &lt;= 0){</font></div><div><font style="font-size: 16pt;">            //没有产品</font></div><div><font style="font-size: 16pt;">            try {</font></div><div><font style="font-size: 16pt;">                this.wait();</font></div><div><font style="font-size: 16pt;">            } catch (InterruptedException e) {</font></div><div><font style="font-size: 16pt;">                e.printStackTrace();</font></div><div><font style="font-size: 16pt;">            }</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        //消费</font></div><div><font style="font-size: 16pt;">        count-- ;</font></div><div><font style="font-size: 16pt;">        chicken = chickens[count];</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        //产品消费了， 通知生产者</font></div><div><font style="font-size: 16pt;">        this.notify();</font></div><div><font style="font-size: 16pt;">        return chicken;</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div></div><div><span style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(255, 0, 0); font-weight: bold;">2.信号灯法</span></span></div><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><font style="font-size: 16pt;">/**</font></div><div><font style="font-size: 16pt;">* 线程通信：</font></div><div><font style="font-size: 16pt;">* 信号灯法</font></div><div><font style="font-size: 16pt;">*/</font></div><div><font style="font-size: 16pt;">public class TestPC02 {</font></div><div><font style="font-size: 16pt;">    public static void main(String[] args) {</font></div><div><font style="font-size: 16pt;">        TV tv = new TV();</font></div><div><font style="font-size: 16pt;">        new Player(tv).start();</font></div><div><font style="font-size: 16pt;">        new Watcher(tv).start();</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">/**</font></div><div><font style="font-size: 16pt;">* 演员</font></div><div><font style="font-size: 16pt;">*/</font></div><div><font style="font-size: 16pt;">class Player extends Thread {</font></div><div><font style="font-size: 16pt;">    private TV tv;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public Player(TV tv) {</font></div><div><font style="font-size: 16pt;">        this.tv = tv;</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public void run() {</font></div><div><font style="font-size: 16pt;">        for (int i = 0; i &lt; 10; i++) {</font></div><div><font style="font-size: 16pt;">            if (i % 2 == 0) {</font></div><div><font style="font-size: 16pt;">                tv.show(&quot;快乐大本营播放中。。。&quot;);</font></div><div><font style="font-size: 16pt;">            }else{</font></div><div><font style="font-size: 16pt;">                tv.show(&quot;抖音：记录美好生活！&quot;);</font></div><div><font style="font-size: 16pt;">            }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">/**</font></div><div><font style="font-size: 16pt;">* 观众</font></div><div><font style="font-size: 16pt;">*/</font></div><div><font style="font-size: 16pt;">class Watcher extends Thread{</font></div><div><font style="font-size: 16pt;">    private TV tv;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public Watcher(TV tv) {</font></div><div><font style="font-size: 16pt;">        this.tv = tv;</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public void run() {</font></div><div><font style="font-size: 16pt;">        for (int i = 0; i &lt; 10; i++) {</font></div><div><font style="font-size: 16pt;">            tv.watch();</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">/**</font></div><div><font style="font-size: 16pt;">* TV</font></div><div><font style="font-size: 16pt;">*/</font></div><div><font style="font-size: 16pt;">class TV {</font></div><div><font style="font-size: 16pt;">    private String voice;</font></div><div><font style="font-size: 16pt;">    private boolean flag = true;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public synchronized void show(String voice) {</font></div><div><font style="font-size: 16pt;">        if (!flag) {</font></div><div><font style="font-size: 16pt;">            //观看中</font></div><div><font style="font-size: 16pt;">            try {</font></div><div><font style="font-size: 16pt;">                this.wait();</font></div><div><font style="font-size: 16pt;">            } catch (InterruptedException e) {</font></div><div><font style="font-size: 16pt;">                e.printStackTrace();</font></div><div><font style="font-size: 16pt;">            }</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">        //表演</font></div><div><font style="font-size: 16pt;">        this.voice = voice;</font></div><div><font style="font-size: 16pt;">        this.flag = !this.flag;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        System.out.println(&quot;演员表演了 &quot;+ this.voice);</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        //叫观众看</font></div><div><font style="font-size: 16pt;">        this.notify();</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public synchronized void watch() {</font></div><div><font style="font-size: 16pt;">        if (flag) {</font></div><div><font style="font-size: 16pt;">            //表演中</font></div><div><font style="font-size: 16pt;">            try {</font></div><div><font style="font-size: 16pt;">                this.wait();</font></div><div><font style="font-size: 16pt;">            } catch (InterruptedException e) {</font></div><div><font style="font-size: 16pt;">                e.printStackTrace();</font></div><div><font style="font-size: 16pt;">            }</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">        //观看</font></div><div><font style="font-size: 16pt;">        this.voice = voice;</font></div><div><font style="font-size: 16pt;">        this.flag = !this.flag;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        System.out.println(&quot;观众观看了 &quot;+this.voice);</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        //看完了， 叫演员表演</font></div><div><font style="font-size: 16pt;">        this.notify();</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div></div><div><span style="font-size: 18pt;"><span style="font-size: 18pt;"><br/></span></span></div><div><span style="font-size: 18pt;"><span style="font-size: 18pt; color: rgb(255, 0, 0); font-weight: bold;">四、线程池</span></span></div><div><span style="font-size: 18pt;"><span style="font-size: 18pt;"><br/></span></span></div><div style="text-align: center;"><span style="font-size: 18pt;"><span style="color: rgb(255, 0, 0); font-size: 18pt; font-weight: bold;"><img src="./thread/Image [2].png" type="image/png" data-filename="Image.png"/></span></span></div><div><span style="font-size: 16pt;">实例代码：</span></div><div><font style="font-size: 16pt;"><br/></font></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><font style="font-size: 16pt;">import java.util.concurrent.ExecutorService;</font></div><div><font style="font-size: 16pt;">import java.util.concurrent.Executors;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">/**</font></div><div><font style="font-size: 16pt;">* 线程池</font></div><div><font style="font-size: 16pt;">*/</font></div><div><font style="font-size: 16pt;">public class TestPool {</font></div><div><font style="font-size: 16pt;">    public static void main(String[] args) {</font></div><div><font style="font-size: 16pt;">        ExecutorService executorService = Executors.newFixedThreadPool(10);</font></div><div><font style="font-size: 16pt;">        executorService.execute(new Print());</font></div><div><font style="font-size: 16pt;">        executorService.execute(new Print());</font></div><div><font style="font-size: 16pt;">        executorService.execute(new Print());</font></div><div><font style="font-size: 16pt;">        executorService.shutdown();</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;">class Print implements Runnable{</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public void run() {</font></div><div><font style="font-size: 16pt;">        System.out.println(Thread.currentThread().getName());</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;">====================================================================================</font></div><div><font style="font-size: 16pt;">import org.apache.commons.io.FileUtils;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">import java.io.File;</font></div><div><font style="font-size: 16pt;">import java.io.IOException;</font></div><div><font style="font-size: 16pt;">import java.net.URL;</font></div><div><font style="font-size: 16pt;">import java.util.concurrent.*;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">public class TestCallable implements Callable&lt;Boolean&gt; {</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    private String url ;</font></div><div><font style="font-size: 16pt;">    private String fileName ;</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public TestCallable(String url, String fileName) {</font></div><div><font style="font-size: 16pt;">        this.url = url;</font></div><div><font style="font-size: 16pt;">        this.fileName = fileName;</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    @Override</font></div><div><font style="font-size: 16pt;">    public Boolean call() throws Exception {</font></div><div><font style="font-size: 16pt;">        DownUtilss.down(url , fileName);</font></div><div><font style="font-size: 16pt;">        System.out.println(Thread.currentThread().getName()+&quot;下载了&quot;+fileName);</font></div><div><font style="font-size: 16pt;">        return true;</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    public static void main(String[] args) throws Exception {</font></div><div><font style="font-size: 16pt;">        TestCallable testCallable = new TestCallable(&quot;https://gitee.com/zhushangxue/blogimg/raw/master/img/20210804182542.png&quot;, &quot;1.png&quot;);</font></div><div><font style="font-size: 16pt;">        TestCallable testCallable1 = new TestCallable(&quot;https://gitee.com/zhushangxue/blogimg/raw/master/img/20210804182542.png&quot;, &quot;2.png&quot;);</font></div><div><font style="font-size: 16pt;">        TestCallable testCallable2 = new TestCallable(&quot;https://gitee.com/zhushangxue/blogimg/raw/master/img/20210804182542.png&quot;, &quot;3.png&quot;);</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        //创建执行服务  线程池</font></div><div><font style="font-size: 16pt;">        ExecutorService executorService = Executors.newFixedThreadPool(3);</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        //提交执行</font></div><div><font style="font-size: 16pt;">        Future&lt;Boolean&gt; submit = executorService.submit(testCallable);</font></div><div><font style="font-size: 16pt;">        Future&lt;Boolean&gt; submit1 = executorService.submit(testCallable1);</font></div><div><font style="font-size: 16pt;">        Future&lt;Boolean&gt; submit2 = executorService.submit(testCallable2);</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        //获取结果</font></div><div><font style="font-size: 16pt;">        Boolean aBoolean = submit.get();</font></div><div><font style="font-size: 16pt;">        Boolean aBoolean1 = submit1.get();</font></div><div><font style="font-size: 16pt;">        Boolean aBoolean2 = submit2.get();</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">        //关闭服务</font></div><div><font style="font-size: 16pt;">        executorService.shutdown();</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;"><br/></font></div><div><font style="font-size: 16pt;">class DownUtilss{</font></div><div><font style="font-size: 16pt;">    public static void down(String url , String fileName){</font></div><div><font style="font-size: 16pt;">        try {</font></div><div><font style="font-size: 16pt;">            FileUtils.copyURLToFile(new URL(url), new File(fileName));</font></div><div><font style="font-size: 16pt;">        } catch (IOException e) {</font></div><div><font style="font-size: 16pt;">            throw new RuntimeException(&quot;下载异常&quot;);</font></div><div><font style="font-size: 16pt;">        }</font></div><div><font style="font-size: 16pt;">    }</font></div><div><font style="font-size: 16pt;">}</font></div></div><div><br/></div></span>
</div></body></html> 